package com.mlan.userap;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import com.mlan.userap.component.FaceManager;
import com.mlan.userap.db.ChatDao;
import com.mlan.userap.service.NotificationService;
import com.mlan.userap.untils.PermissionHelper;
import com.mlan.userap.untils.SPUtils;

public class MyAPP extends Application {

    private static Context context;
    private static MyAPP myAPP;

    public static Context getContext() {
        return context;
    }

    public static MyAPP getApp() {
        return myAPP;
    }

    private PermissionHelper permissionHelper;
    private static ChatDao chatDao;

    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
        SPUtils.init(context);
        myAPP = this;
        permissionHelper = new PermissionHelper();
        chatDao = new ChatDao(context);

        // Start notification service
        startService(new Intent(this, NotificationService.class));
    }

    public ChatDao getChatDao() {
        return chatDao;
    }

    public PermissionHelper getPermissionHelper() {
        return permissionHelper;
    }

    public void showToast(String content) {
        if (content.length() > 0) {
            new Handler(Looper.getMainLooper()).post(() ->
                    Toast.makeText(this, content, Toast.LENGTH_SHORT).show()
            );
            Log.e("myApp", content);
        }
    }

    public void showNotification(String title, String message, String groupId) {
        Intent intent = new Intent(this, NotificationService.class);
        intent.putExtra("title", title);
        intent.putExtra("message", message);
        intent.putExtra("groupId", groupId);
        startService(intent);
    }

    private Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            /**
             * 获取本机应用
             */
            if (msg.what == 1) {
                // PostAppInfo();
            }
            /**
             * 获取屏
             */
            if (msg.what == 2) {
                //  PostScreen();
            }
        }
    };

    public void SendMsg(int _what) {
        Message msg = mHandler.obtainMessage();
        msg.what = _what;
        mHandler.sendMessage(msg);
    }

    private void runOnUiThread(Object o) {
    }
}
